tk scaling 1.2

#win setup
wm title . "asmCalc"
wm resizable . 0 0

ttk::style layout bitflag.TCheckbutton {
	Checkbutton.indicator
	Checkbutton.focus
}

ttk::style layout cpuflags.TCheckbutton {
	Checkbutton.indicator -side top -sticky e 
	Checkbutton.label
}

proc createGui { } {
	
	#commands frame
	ttk::frame .cmd
	#little-endian
	ttk::labelframe .cmd.host -text "Host" -padding 1
	ttk::label      .cmd.host.endian -textvariable hostEndianText -anchor center -relief sunken -width 5
	ttk::combobox   .cmd.host.regsize -values { "8bit" "16bit" "32bit" "64bit" } -width 4 -textvariable regSize
	bind .cmd.host.regsize <<ComboboxSelected>> { setRegSize; updateMainLayout }
	grid .cmd.host.endian  -row 0 -column 0 -padx 1
	grid .cmd.host.regsize -row 1 -column 0 -padx 1
	
	ttk::labelframe  .cmd.flags    -text "CPU flags" -padding 1
	ttk::checkbutton .cmd.flags.cf -text "cf" -style cpuflags.TCheckbutton -takefocus 0 -class readonly -variable cpuFlagCf
	ttk::checkbutton .cmd.flags.pf -text "pf" -style cpuflags.TCheckbutton -takefocus 0 -class readonly -variable cpuFlagPf
	ttk::checkbutton .cmd.flags.af -text "af" -style cpuflags.TCheckbutton -takefocus 0 -class readonly -variable cpuFlagAf
	ttk::checkbutton .cmd.flags.zf -text "zf" -style cpuflags.TCheckbutton -takefocus 0 -class readonly -variable cpuFlagZf
	ttk::checkbutton .cmd.flags.sf -text "sf" -style cpuflags.TCheckbutton -takefocus 0 -class readonly -variable cpuFlagSf
	ttk::checkbutton .cmd.flags.of -text "of" -style cpuflags.TCheckbutton -takefocus 0 -class readonly -variable cpuFlagOf
	grid .cmd.flags.cf -row 0 -column 0 -padx 3
	grid .cmd.flags.pf -row 0 -column 1 -padx 3
	grid .cmd.flags.af -row 0 -column 2 -padx 3
	grid .cmd.flags.zf -row 0 -column 3 -padx 3
	grid .cmd.flags.sf -row 0 -column 4 -padx 3
	grid .cmd.flags.of -row 0 -column 5 -padx 3
	
	ttk::labelframe .cmd.math      -text "Arithmetic" -padding 1
	ttk::button     .cmd.math.add  -text "add"  -width 3 -padding 0 -command opAdd
	ttk::button     .cmd.math.sub  -text "sub"  -width 3 -padding 0 -command opSub
	ttk::button     .cmd.math.mul  -text "mul"  -width 3 -padding 0 -command opMul
	ttk::button     .cmd.math.div  -text "div"  -width 3 -padding 0 -command opDiv
	ttk::button     .cmd.math.imul -text "imul" -width 4 -padding 0 -command opImul
	ttk::button     .cmd.math.idiv -text "idiv" -width 4 -padding 0 -command opIdiv
	ttk::button     .cmd.math.neg  -text "neg"  -width 3 -padding 0 -command opNeg
	grid .cmd.math.add  -row 0 -column 0
	grid .cmd.math.sub  -row 1 -column 0
	grid .cmd.math.mul  -row 0 -column 1
	grid .cmd.math.div  -row 1 -column 1
	grid .cmd.math.imul -row 0 -column 2
	grid .cmd.math.idiv -row 1 -column 2
	grid .cmd.math.neg  -row 0 -column 3
	
	ttk::labelframe .cmd.logic     -text "Logic" -padding 1
	ttk::button     .cmd.logic.and -text "and" -width 3 -padding 0 -command opAnd
	ttk::button     .cmd.logic.not -text "not" -width 3 -padding 0 -command opNot
	ttk::button     .cmd.logic.or  -text "or"  -width 3 -padding 0 -command opOr
	ttk::button     .cmd.logic.xor -text "xor" -width 3 -padding 0 -command opXor
	grid .cmd.logic.and -row 0 -column 0
	grid .cmd.logic.not -row 1 -column 0
	grid .cmd.logic.or  -row 0 -column 1
	grid .cmd.logic.xor -row 1 -column 1
	
	ttk::labelframe .cmd.shift     -text "Shift" -padding 1
	ttk::button     .cmd.shift.shl -text "shl" -width 3 -padding 0 -command opShl
	ttk::button     .cmd.shift.shr -text "shr" -width 3 -padding 0 -command opShr
	ttk::button     .cmd.shift.sal -text "sal" -width 3 -padding 0 -command opSal
	ttk::button     .cmd.shift.sar -text "sar" -width 3 -padding 0 -command opSar
	ttk::button     .cmd.shift.rol -text "rol" -width 3 -padding 0 -command opRol
	ttk::button     .cmd.shift.ror -text "ror" -width 3 -padding 0 -command opRor
	grid .cmd.shift.shl -row 0 -column 0
	grid .cmd.shift.shr -row 1 -column 0
	grid .cmd.shift.sal -row 0 -column 1
	grid .cmd.shift.sar -row 1 -column 1
	grid .cmd.shift.rol -row 0 -column 2
	grid .cmd.shift.ror -row 1 -column 2
	
	ttk::labelframe .cmd.other -text "Other" -padding 1
	ttk::button     .cmd.other.bswap -text "bswap" -width 6 -padding 0 -command opBswap
	ttk::button     .cmd.other.movsx -text "movsx" -width 6 -padding 0 -command opMovsx
	grid .cmd.other.bswap -row 0 -column 0 -padx 1
	grid .cmd.other.movsx -row 1 -column 0 -padx 1
	
	#output reg selector
	ttk::labelframe  .cmd.output    -text "Out" -padding 1
	ttk::radiobutton .cmd.output.r0 -text "R0" -variable outputReg -value 0 -command setOutputReg
	ttk::radiobutton .cmd.output.r2 -text "R2" -variable outputReg -value 2 -command setOutputReg
	grid .cmd.output.r0 -row 0 -column 0
	grid .cmd.output.r2 -row 1 -column 0
	
	#attach all to cmd frame
	grid .cmd.host    -row 0 -column 0 -padx 1
	grid .cmd.flags   -row 0 -column 1 -padx 1
	grid .cmd.math    -row 0 -column 2 -padx 1
	grid .cmd.logic   -row 0 -column 3 -padx 1
	grid .cmd.shift   -row 0 -column 4 -padx 1
	grid .cmd.other   -row 0 -column 5 -padx 1
	grid .cmd.output  -row 0 -column 6 -padx 1
	
	ttk::frame .regs
	
	#reg blocks
	for { set regno 0 } { $regno < 3 } { incr regno } {
		
		ttk::labelframe .regs.reg$regno -text "R$regno" -padding 1
		
		ttk::frame  .regs.reg$regno.vars
		ttk::button .regs.reg$regno.vars.rst -text "rst" -width 3 -padding 0 -command "regRst $regno"
		ttk::label  .regs.reg$regno.vars.siglab
		ttk::label  .regs.reg$regno.vars.unsiglab
		ttk::label  .regs.reg$regno.vars.hexlab   -text "Hex:"
		ttk::entry  .regs.reg$regno.vars.sigval   -validate key -validatecommand "editReg $regno s %P" -textvariable reg${regno}s -width 22 -justify center
		ttk::entry  .regs.reg$regno.vars.unsigval -validate key -validatecommand "editReg $regno u %P" -textvariable reg${regno}u -width 21 -justify center
		ttk::entry  .regs.reg$regno.vars.hexval   -validate key -validatecommand "editReg $regno h %P" -textvariable reg${regno}h -width 40 -justify center
		
		#bytes and bits
		ttk::frame .regs.reg$regno.bytes
		for { set byteno 0 } { $byteno < 8 } { incr byteno } {
			ttk::labelframe .regs.reg$regno.bytes.byte$byteno -text "Byte $byteno"
			for { set bitno 0; set bitcolno 7; } { $bitno < 8 } { incr bitno; incr bitcolno -1; } {
				ttk::checkbutton .regs.reg$regno.bytes.byte$byteno.bit$bitno -variable reg${regno}b${byteno}${bitno} -command  "editRegByte $regno $byteno $bitno" -style bitflag.TCheckbutton
				grid .regs.reg$regno.bytes.byte$byteno.bit$bitno -row 0 -column $bitcolno
			}
		}
		
		#attach all to reg block
		grid .regs.reg$regno.vars  -row 0 -column 0
		grid .regs.reg$regno.bytes -row 1 -column 0
		
	} ; #end of reg blocks for()
	
	#attach all to windows
	grid .cmd  -row 0 -column 0 -padx 1
	grid .regs -row 1 -column 0 -padx 1
	updateMainLayout
}

proc regLayoutRow { regno } {
	grid .regs.reg$regno.vars.rst      -row 0 -column 0 -padx 2
	grid .regs.reg$regno.vars.siglab   -row 0 -column 1 -padx 2
	grid .regs.reg$regno.vars.sigval   -row 0 -column 2 -padx 2
	grid .regs.reg$regno.vars.unsiglab -row 0 -column 3 -padx 2
	grid .regs.reg$regno.vars.unsigval -row 0 -column 4 -padx 2
	grid .regs.reg$regno.vars.hexlab   -row 0 -column 5 -padx 2
	grid .regs.reg$regno.vars.hexval   -row 0 -column 6 -padx 2
	grid .regs.reg$regno          -row $regno -column 0 -padx 1
}


proc regLayoutCol { regno } {
	grid .regs.reg$regno.vars.siglab   -row 0 -column 0 -padx 2
	grid .regs.reg$regno.vars.sigval   -row 0 -column 1 -padx 2
	grid .regs.reg$regno.vars.unsiglab -row 1 -column 0 -padx 2
	grid .regs.reg$regno.vars.unsigval -row 1 -column 1 -padx 2
	grid .regs.reg$regno.vars.rst      -row 0 -column 3 -padx 2
	grid .regs.reg$regno.vars.hexlab   -row 1 -column 2 -padx 2
	grid .regs.reg$regno.vars.hexval   -row 1 -column 3 -padx 2
	grid .regs.reg$regno          -row 0 -column $regno -padx 1
}

proc updateMainLayout { } {
	
	global regSize
	
	if { $regSize == "64bit" } {
		for { set regno 0 } { $regno < 3 } { incr regno } {
			.regs.reg$regno.vars.siglab   configure -text "s64:"
			.regs.reg$regno.vars.unsiglab configure -text "u64:"
			.regs.reg$regno.vars.sigval   configure -width 22
			.regs.reg$regno.vars.unsigval configure -width 21
			.regs.reg$regno.vars.hexval   configure -width 40
			
			regLayoutRow $regno
			
			for { set byteno 0 } { $byteno < 8 } { incr byteno } {
				grid .regs.reg$regno.bytes.byte$byteno -row 0 -column $byteno -padx 1
			}
		}
	} elseif { $regSize == "32bit" } {
		for { set regno 0 } { $regno < 3 } { incr regno } {
			
			.regs.reg$regno.vars.siglab   configure -text "s32:"
			.regs.reg$regno.vars.unsiglab configure -text "u32:"
			.regs.reg$regno.vars.sigval   configure -width 13
			.regs.reg$regno.vars.unsigval configure -width 12
			.regs.reg$regno.vars.hexval   configure -width 20
			
			regLayoutRow $regno
			
			set byteno 0
			for {  } { $byteno < 4 } { incr byteno } {
				grid .regs.reg$regno.bytes.byte$byteno -row 0 -column $byteno -padx 1
			}
			
			for {  } { $byteno < 8 } { incr byteno } {
				grid forget .regs.reg$regno.bytes.byte$byteno
			}
		}
	} elseif { $regSize == "16bit" } {
		for { set regno 0 } { $regno < 3 } { incr regno } {
			.regs.reg$regno.vars.siglab   configure -text "s16:"
			.regs.reg$regno.vars.unsiglab configure -text "u16:"
			.regs.reg$regno.vars.sigval   configure -width 7
			.regs.reg$regno.vars.unsigval configure -width 6
			.regs.reg$regno.vars.hexval   configure -width 11
			
			regLayoutCol $regno
			
			set byteno 0
			for {  } { $byteno < 2 } { incr byteno } {
				grid .regs.reg$regno.bytes.byte$byteno -row 0 -column $byteno -padx 1
			}
			
			for {  } { $byteno < 8 } { incr byteno } {
				grid forget .regs.reg$regno.bytes.byte$byteno
			}
		}
	} elseif { $regSize == "8bit" } {
		for { set regno 0 } { $regno < 3 } { incr regno } {
			
			.regs.reg$regno.vars.siglab   configure -text "s8:"
			.regs.reg$regno.vars.unsiglab configure -text "u8:"
			.regs.reg$regno.vars.sigval   configure -width 5
			.regs.reg$regno.vars.unsigval configure -width 4
			.regs.reg$regno.vars.hexval   configure -width 5
			
			regLayoutCol $regno
			
			set byteno 0
			for {  } { $byteno < 1 } { incr byteno } {
				grid .regs.reg$regno.bytes.byte$byteno -row 0 -column $byteno -padx 1
			}
			
			for {  } { $byteno < 8 } { incr byteno } {
				grid forget .regs.reg$regno.bytes.byte$byteno
			}
		}
	}
}

#key binding
bind . <Control-q> { destroy . }

createGui
set outputReg 0

set cpuFlagCf 0
set cpuFlagPf 0
set cpuFlagAf 0
set cpuFlagZf 0
set cpuFlagSf 0
set cpuFlagOf 0
